package com.canplay.screen.utils

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.graphics.Point
import android.graphics.Rect
import android.os.Build
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.View
import android.view.WindowManager
import android.widget.TextView

object ScreenUtils {

    fun getScreenWidth(context: Context): Int {
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val outMetrics = DisplayMetrics()
        wm.defaultDisplay.getMetrics(outMetrics)
        return outMetrics.widthPixels
    }

    fun getScreenHeight(context: Context): Int {
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val outMetrics = DisplayMetrics()
        wm.defaultDisplay.getMetrics(outMetrics)
        return outMetrics.heightPixels
    }

    fun isShowNavBar(context: Context?): Boolean {
        if (null == context) {
            return false
        }
        val outRect1 = Rect()
        try {
            (context as Activity).window.decorView.getWindowVisibleDisplayFrame(outRect1)
        } catch (e: ClassCastException) {
            e.printStackTrace()
            return false
        }
        val activityHeight = outRect1.height()

        /**
         * 获取状态栏高度
         */
        val statusBarHeight = getStatusBarHeight(context)

        /**
         * 屏幕物理高度 减去 状态栏高度
         */
        val remainHeight = getRealHeight(context) - statusBarHeight
        /**
         * 剩余高度跟应用区域高度相等 说明导航栏没有显示 否则相反
         */
        return activityHeight != remainHeight
    }

    /**
     * 获取真实屏幕高度
     *
     * @return
     */
    fun getRealHeight(context: Context): Int {
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val point = Point()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.defaultDisplay.getRealSize(point)
        } else {
            wm.defaultDisplay.getSize(point)
        }
        return point.y
    }

    fun getStatusBarHeight(context: Context): Int {
        var result = 0
        val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return result
    }

    fun getNavigationBarHeight(context: Context): Int {
        var result = 0
        val resourceId: Int =
            context.resources.getIdentifier("navigation_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = context.resources.getDimensionPixelSize(resourceId)
        }
        return result
    }

    fun setTransparentWindow(activity: Activity) {
        activity.window.statusBarColor = Color.TRANSPARENT
        activity.window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    }


    fun setScaledSizeSP(
        textView: TextView, resources: Resources, targetWidthDp: Int, originalFontSizeSp: Float
    ) {
        val screenWidthPx = resources.displayMetrics.widthPixels
        val density = resources.displayMetrics.density
        val targetWidthPx = (targetWidthDp * density).toInt()
        val scaleFactor = screenWidthPx.toFloat() / targetWidthPx
        val scaledFontSizeSp = originalFontSizeSp * scaleFactor
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, scaledFontSizeSp)
    }

    fun getResultDP(resources: Resources, targetWidthDp: Int): Float {
        val stander = 1080
        //获取屏幕宽的像素 横屏
        val screenWidthPx = resources.displayMetrics.heightPixels
        //获取屏幕密度
        val density = resources.displayMetrics.density
        //已有dp转换成px
        val targetWidthPx = (targetWidthDp * density).toInt()

        val resultPx = screenWidthPx.toFloat() * targetWidthPx / stander
        return (resultPx / density);
    }

    fun dp2Px(resources: Resources, resourceDp: Int): Int {
        //获取屏幕密度
        val density = resources.displayMetrics.density
        return (resourceDp * density).toInt()
    }
}